/**
 * 
 */
package com.kstech.exccheck.util;
import android.app.Activity;
import android.content.Context;

import com.kstech.exccheck.config.DeviceModelFile;
import com.kstech.exccheck.config.ResConfigFile;
import com.kstech.exccheck.config.vo.CheckLineVO;
import com.kstech.exccheck.domain.checkline.CheckLineManager;
import com.kstech.exccheck.domain.user.User;
import com.kstech.exccheck.exception.ExcException;

import java.util.ArrayList;
import java.util.List;

/**
 * App上下文，记录全局信息
 * @author tan
 *
 */
public class AppContext {

	public static Context context;

	/**
	 * 当前登录的用户
	 */
	private static User currentUser;

	/**
	 * 当前检线信息
	 */
	private static CheckLineVO currentCheckLine;

	/**
	 * 对应res_config.xml配置文件的内容
	 */
	private static ResConfigFile resConfig;

	/**
	 * 当前选中的机型对应的机型配置文件
	 */
	private static DeviceModelFile modelFile;

	/**
	 * 当前系统Activity信息
	 */
	private static List<Activity> acitvityList = new ArrayList<Activity>();

	/**
	 * 加载当前使用的机型文件
	 * @param deviceId 机型ID
	 * @param subDeviceId 子机型ID
	 * @throws ExcException
	 */
	public static void loadDeviceModelFile(String deviceId,String subDeviceId) throws ExcException {
		String fileName = deviceId;
		if (null !=subDeviceId && !"".equals(subDeviceId)) {
			fileName = fileName + "%" + subDeviceId;
		}
		modelFile = DeviceModelFile.readFromFile(fileName+".xml");
	}

	public static void initResource() {
		resConfig = ResConfigFile.readFromFile();
		currentCheckLine = CheckLineManager.getDefaultCheckLine();
	}

	public static DeviceModelFile getModelFile() {
		return modelFile;
	}

	public static void setModelFile(DeviceModelFile modelFile) {
		AppContext.modelFile = modelFile;
	}

	public static ResConfigFile getResConfig() {
		return resConfig;
	}

	public static CheckLineVO getCurrentCheckLine() {
		return currentCheckLine;
	}

	public static void setCurrentCheckLine(CheckLineVO checkLine) {
		AppContext.currentCheckLine = checkLine;
	}

	public static User getCurrentUser() {
		return currentUser;
	}

	public static void setCurrentUser(User currentUser) {
		AppContext.currentUser = currentUser;
	}

	public static void addActivity(Activity acitvity) {
		acitvityList.add(acitvity);
	}

	/**
	 * 退出系统，结束所有activity的生命周期
	 */
	public static void exitSystem() {
		for (Activity acitvity : acitvityList) {
			acitvity.finish();
		}
	};
}

